package com.tuanzi.loan.business.repository.product.space.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_PRODUCT_INFO_NEW;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductSpace.LOAN_PRODUCT_SPACE;

import com.alibaba.fastjson.JSON;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.service.system.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.product.space.custom.LoanProductSpaceRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.product.space.LoanProductSpaceSearchRequest;
import com.tuanzi.loan.vo.response.product.space.LoanProductSpaceSearchResponse;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class LoanProductSpaceRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanProductSpaceRepositoryCustom {

	@Autowired
	private DictionaryService dictionaryService;


	@Override
	protected TableImpl getTable() {
		return LOAN_PRODUCT_SPACE;
	}

	@Override
	public Page<LoanProductSpaceSearchResponse> list(LoanProductSpaceSearchRequest searchRequest, Pageable pageable) {
		fields(LOAN_PRODUCT_SPACE.fields());
		contains(LOAN_PRODUCT_SPACE.NAME, searchRequest.getName());
		contains(LOAN_PRODUCT_SPACE.LARGE_COMMON_PRODUCT, searchRequest.getParentPrdid());
		eq(LOAN_PRODUCT_SPACE.TYPE, searchRequest.getType());
		orderBy(LOAN_PRODUCT_SPACE.CREATE_TIME, Sort.Direction.DESC);
		return search(pageable, (record) -> {
			LoanProductSpaceSearchResponse response = new LoanProductSpaceSearchResponse();
			response.setId(record.get(LOAN_PRODUCT_SPACE.ID));
			response.setSpaceId(record.get(LOAN_PRODUCT_SPACE.SPACE_ID));
			response.setName(record.get(LOAN_PRODUCT_SPACE.NAME));
			response.setType(record.get(LOAN_PRODUCT_SPACE.TYPE));
			response.setShowCount(record.get(LOAN_PRODUCT_SPACE.SHOW_COUNT));
			response.setLevel(record.get(LOAN_PRODUCT_SPACE.LEVEL));
			response.setUvValueRatio(record.get(LOAN_PRODUCT_SPACE.UV_VALUE_RATIO));
			response.setRemainLimitRatio(record.get(LOAN_PRODUCT_SPACE.REMAIN_LIMIT_RATIO));
			response.setExposureRatio(record.get(LOAN_PRODUCT_SPACE.EXPOSURE_RATIO));
			response.setCreateTime(record.get(LOAN_PRODUCT_SPACE.CREATE_TIME));
			response.setUpdateTime(record.get(LOAN_PRODUCT_SPACE.UPDATE_TIME));
			response.setUpdator(record.get(LOAN_PRODUCT_SPACE.UPDATOR));
			String large_product = record.get(LOAN_PRODUCT_SPACE.LARGE_COMMON_PRODUCT);
			Map<String, String> parentProdByCode = dictionaryService.listMapByCode(DictionaryCode.LARGE_COMMON_PRODUCT);
			if (parentProdByCode != null){
				response.setLarge_products(parentProdByCode.get(large_product));
			}
			return response;
		});
	}

}
